home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zcolor2.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  213 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zcolor2.c */
  20. /* Level 2 color operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gscolor.h"
  25. #include "gxcolor.h"
  26. #include "gscspace.h"
  27. #include "gscolor2.h"
  28. #include "gsmatrix.h"
  29. #include "dict.h"
  30. #include "dparam.h"
  31. #include "iname.h"        /* for name_eq */
  32. #include "state.h"
  33. #include "store.h"
  34.  
  35. /* Forward references */
  36. private int store_color_params(P3(os_ptr, const gs_paint_color *, const gs_color_space *));
  37. private int load_color_params(P3(os_ptr, gs_paint_color *, const gs_color_space *));
  38.  
  39. /* Names of keys in pattern dictionaries: */
  40. static ref name_PatternType;
  41. static ref name_PaintType;
  42. static ref name_TilingType;
  43. static ref name_BBox;
  44. static ref name_XStep;
  45. static ref name_YStep;
  46. static ref name_PaintProc;
  47. static ref name_Implementation;
  48.  
  49. /* Initialization */
  50. private void
  51. zcolor2_init(void)
  52. {    static const names_def patn[] = {
  53.  
  54.     /* Create the names of the known entries in */
  55.     /* a pattern dictionary. */
  56.        { "PatternType", &name_PatternType },
  57.        { "PaintType", &name_PaintType },
  58.        { "TilingType", &name_TilingType },
  59.        { "BBox", &name_BBox },
  60.        { "XStep", &name_XStep },
  61.        { "YStep", &name_YStep },
  62.        { "PaintProc", &name_PaintProc },
  63.        { "Implementation", &name_Implementation },
  64.  
  65.     /* Mark the end of the initialized name list. */
  66.        names_def_end
  67.     };
  68.  
  69.     init_names(patn);
  70. }
  71.  
  72.  
  73. /* - currentcolor <param1> ... <paramN> */
  74. int
  75. zcurrentcolor(register os_ptr op)
  76. {    const gs_client_color *pc = gs_currentcolor(igs);
  77.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  78.     int n;
  79.     check_ostack(4);
  80.     if ( pcs->type->index == gs_color_space_index_Pattern )
  81.     {    n = 1;
  82.         if ( pc->pattern.PaintType == 2 )
  83.             n += store_color_params(op, &pc->paint,
  84.                 (const gs_color_space *)&pcs->params.pattern.base_space);
  85.         op[n] = istate.pattern;
  86.     }
  87.     else
  88.         n = store_color_params(op, &pc->paint, pcs);
  89.     push(n);
  90.     return 0;
  91. }
  92.  
  93. /* - currentoverprint <bool> */
  94. int
  95. zcurrentoverprint(register os_ptr op)
  96. {    push(1);
  97.     make_bool(op, gs_currentoverprint(igs));
  98.     return 0;
  99. }
  100.  
  101. /* <pattern> <matrix> makepattern <newpattern> */
  102. int
  103. zmakepattern(os_ptr op)
  104. {    os_ptr op1 = op - 1;
  105.     int code;
  106.     gs_matrix mat;
  107.     int PatternType;
  108.     float BBox[4];
  109.     gs_pattern pattern;
  110.     ref *pPaintProc;
  111.     check_type(*op1, t_dictionary);
  112.     check_dict_read(*op1);
  113.     if ( (code = read_matrix(op, &mat)) < 0 ||
  114.          (code = dict_int_param(op1, &name_PatternType, 1, 1, 0, &PatternType)) < 0 ||
  115.          (code = dict_int_param(op1, &name_PaintType, 1, 2, 0, &pattern.PaintType)) < 0 ||
  116.          (code = dict_int_param(op1, &name_TilingType, 1, 3, 0, &pattern.TilingType)) < 0 ||
  117.          (code = dict_float_array_param(op1, &name_BBox, 4, BBox, NULL)) != 4 ||
  118.          (code = dict_float_param(op1, &name_XStep, 0.0, &pattern.XStep)) != 0 ||
  119.          pattern.XStep == 0.0 ||
  120.          (code = dict_float_param(op1, &name_YStep, 0.0, &pattern.YStep)) != 0 ||
  121.          pattern.YStep == 0.0 ||
  122.          (code = dict_find(op1, &name_PaintProc, &pPaintProc)) <= 0
  123.        )
  124.         return_error((code < 0 ? code : e_rangecheck));
  125.     check_proc(*pPaintProc);
  126.     pattern.BBox.p.x = min(BBox[0], BBox[2]);
  127.     pattern.BBox.p.y = min(BBox[1], BBox[3]);
  128.     pattern.BBox.q.x = max(BBox[0], BBox[2]);
  129.     pattern.BBox.q.y = max(BBox[1], BBox[3]);
  130.     /****** FINISH ******/
  131.     NYI("makepattern");
  132.     pop(1);
  133.     return 0;
  134. }
  135.  
  136. /* <param1> ... <paramN> setcolor - */
  137. int
  138. zsetcolor(register os_ptr op)
  139. {    gs_client_color c;
  140.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  141.     int n, code;
  142.     if ( pcs->type->index == gs_color_space_index_Pattern )
  143.     {    /* Make sure *op is a real Pattern. */
  144.         ref *pImpl;
  145.         const gs_pattern *ppat;
  146.         if ( dict_find(op, &name_Implementation, &pImpl) <= 0 ||
  147.              !r_has_type(pImpl, t_string) ||
  148.              r_size(pImpl) != sizeof(gs_pattern)
  149.            )
  150.             return_error(e_rangecheck);
  151.         ppat = (const gs_pattern *)pImpl->value.const_bytes;
  152.         c.pattern = *ppat;
  153.         if ( c.pattern.PaintType == 2 )
  154.         {    n = load_color_params(op - 1, &c.paint,
  155.                 (const gs_color_space *)&pcs->params.pattern.base_space);
  156.             if ( n < 0 ) return n;
  157.             n++;
  158.         }
  159.         else
  160.             n = 1;
  161.     }
  162.     else
  163.         n = load_color_params(op, &c.paint, pcs);
  164.     if ( n < 0 ) return n;
  165.     code = gs_setcolor(igs, &c);
  166.     if ( code < 0 ) return code;
  167.     pop(n);
  168.     return 0;
  169. }
  170.  
  171. /* <bool> setoverprint - */
  172. int
  173. zsetoverprint(register os_ptr op)
  174. {    check_type(*op, t_boolean);
  175.     gs_setoverprint(igs, op->value.index);
  176.     pop(1);
  177.     return 0;
  178. }
  179.  
  180. /* ------ Initialization procedure ------ */
  181.  
  182. op_def zcolor2_op_defs[] = {
  183.     {"0currentcolor", zcurrentcolor},
  184.     {"0currentoverprint", zcurrentoverprint},
  185.     {"2makepattern", zmakepattern},
  186.     {"1setcolor", zsetcolor},
  187.     {"1setoverprint", zsetoverprint},
  188.     op_def_end(zcolor2_init)
  189. };
  190.  
  191. /* ------ Internal procedures ------ */
  192.  
  193. /* Store non-pattern color values on the operand stack. */
  194. /* Return the number of values stored. */
  195. private int
  196. store_color_params(os_ptr op, const gs_paint_color *pc,
  197.   const gs_color_space *pcs)
  198. {    int n = pcs->type->num_components;
  199.     make_reals(op + 1, pc->values, n);
  200.     return n;
  201. }
  202.  
  203. /* Load non-pattern color values from the operand stack. */
  204. /* Return the number of values stored. */
  205. private int
  206. load_color_params(os_ptr op, gs_paint_color *pc,
  207.   const gs_color_space *pcs)
  208. {    int n = pcs->type->num_components;
  209.     int code = num_params(op, n, pc->values);
  210.     if ( code < 0 ) return code;
  211.     return n;
  212. }
  213.